home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
QRZ! Ham Radio 8
/
QRZ Ham Radio Callsign Database - Volume 8.iso
/
pc
/
files
/
t_unix
/
bs941029.tgz
/
bbsx-941029.tar
/
bbsx
/
bbsrenum.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-10-29
|
4KB
|
189 lines
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <sys/types.h>
#include <pwd.h>
#include <fcntl.h>
#include "bbs.h"
#include "bbs.hd"
#define PASSWORD "/etc/passwd"
const struct cmdtable cmdtable[1];
struct user_seq {
int uid;
int seq;
} ;
struct user_seq user_seq[65000];
void Make_parent_directories(const char *filename);
/*---------------------------------------------------------------------------*/
void Make_parent_directories(const char *filename)
{
char *p, dirname[1024];
strcpy(dirname, filename);
p = strrchr(dirname, '/');
if (!p) halt();
*p = 0;
if (!mkdir(dirname, 0755)) return;
if (errno == EEXIST) return ;
if (errno != ENOENT) halt();
Make_parent_directories(dirname);
if (mkdir(dirname, 0755)) halt();
}
/*---------------------------------------------------------------------------*/
char *Xgetfilename(int mesg, int *comp)
{
static char buf[12];
char cname[50];
char comm[50];
FILE *fp;
*comp =0;
sprintf(buf,
"%02x/%02x/%02x/%02x",
(mesg >> 24) & 0xff,
(mesg >> 16) & 0xff,
(mesg >> 8) & 0xff,
(mesg ) & 0xff);
strcpy(cname,buf);
strcat(cname,compressext);
if (fp=fopen(cname,"r"))
{
fclose(fp);
*comp=1;
return cname;
}
return buf;
}
main() {
FILE *pwdfp, *fp, *proto;
struct passwd *pw;
struct index index;
char *p;
char line[1024];
int count = 0;
int new_nr = 1;
int i,n;
int is_compressed;
int old_ind, new_ind;
char ofilename[1024], nfilename[1024];
if (getuid()) {
perror("permission denied");
return 1;
}
if (chdir(WRKDIR)) {
mkdir(WRKDIR, 0755);
if (chdir(WRKDIR)) halt();
}
read_config();
if ((pwdfp =fopen(PASSWORD,"r")) == NULL) {
perror("error in opening" PASSWORD);
return;
}
if ((proto =fopen("renum.proto","w")) == NULL) {
perror("error in opening renum.proto");
return;
}
p= __pwdalloc();
while(pw =__pwdread(pwdfp, p)) {
sprintf(line,"%s/%s",pw->pw_dir,SEQFILE);
if (fp=fopen(line,"r")) {
fread(line,sizeof(line),1,fp);
user_seq[count].uid = pw->pw_uid;
user_seq[count].seq = atoi(line);
count++;
fclose(fp);
}
}
fclose(pwdfp);
sprintf(line,"%s.o",INDEXFILE);
if(link(INDEXFILE, line) == -1) {
perror("can't rename old indexfile");
return;
}
if(unlink(INDEXFILE) == -1) {
perror("can't remove old indexfile");
return;
}
if ((new_ind = open(INDEXFILE, O_RDWR | O_CREAT, 0644)) < 0) {
perror("can't open new indexfile");
return;
}
if ((old_ind = open(line, O_RDONLY, 0644)) < 0) {
perror("can't open old indexfile");
return;
}
for (;;) {
n=read(old_ind,&index, sizeof(struct index));
if (n < 1) break;
if (!(index.flags & DELETED)) {
strcpy(ofilename,Xgetfilename(index.mesg,&is_compressed));
Make_parent_directories(getfilename(new_nr));
strcpy(nfilename,Xgetfilename(new_nr,&n));
if (is_compressed) strcat(nfilename,compressext);
if(link(ofilename, nfilename) == -1) {
printf("can't rename %s to %s error: %d\n",ofilename, nfilename,
errno);
return;
}
if(unlink(ofilename) == -1) {
printf("can't remove %s\n",ofilename);
return;
}
for(i=0; i<= count; i++)
if(user_seq[i].seq && user_seq[i].seq <= index.mesg) {
pw=getpwuid(user_seq[i].uid);
sprintf(line,"%s/%s",pw->pw_dir,SEQFILE);
if (fp=fopen(line,"w")) {
fprintf(fp,"%d\n",new_nr);
fclose(fp);
chown(line,pw->pw_uid,pw->pw_gid);
}
user_seq[i].seq = 0;
}
fprintf(proto,"%d\t%d\n",index.mesg,new_nr);
index.mesg=new_nr;
if (write(new_ind, &index, sizeof(index)) != sizeof(index)) {
perror("can't write to newindexfile");
return;
}
new_nr++;
}
}
close(old_ind);
close(new_ind);
fclose(proto);
return;
}